package org.reborn.dao

import org.reborn.model._
import org.reborn.model.TileLOD._
import java.util.Date

class InMemoryTerrainColumnDao extends TerrainColumnDao{

  private var tilemap:Map[(Int,Int,TileLOD),Tile] = Map();
  
  def setTile(tile:Tile):Unit = {
    val lod = tile.lod
    require(lod == High)
    
    val x = tile.xIndex
    val y = tile.yIndex
    var origTile = tilemap.get((x,y,lod))
    if(origTile == None){
      val newTile = new Tile(x,y,lod)
      tilemap += (((x,y,lod),newTile))
      origTile = Some(tile)
    }
    
    origTile.get.setColumn(tile.getColumn())
    origTile.get.lastUpdate = Some(new Date())
  }
  
  def getTiles(tiles:Iterable[Tile]):Unit = {
    for(tile <- tiles){
      val storedTile = tilemap(tile.xIndex,tile.yIndex,tile.lod)
      if(storedTile.lastUpdate.isEmpty 
         || storedTile.lastUpdate.get.after(tile.lastUpdate.get)){
        tile.setColumn(storedTile.getColumn)
        tile.lastUpdate = storedTile.lastUpdate
      }
    }
  }
}
